package br.com.amadeus.android.activities.course.goal;


import br.com.amadeus.android.activities.home.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

public class ChartGoalActivity extends Activity {
	
	private static final int NOVO = 0;
	private static final int LISTAGEM = 1;
	
	private Intent itListGoals;
	private Intent itNewGoal; 
	
	private int course_id;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Toast.makeText(ChartGoalActivity.this, "Aguarde...", Toast.LENGTH_LONG);
		final WebView googleChartView = new WebView(this);
		setContentView(googleChartView);
		/**
		 * Valores passados ao Google API
		 * 
		 * 1. Quantidade de horas do Objetivo Planejado
		 * 2. Quantidade de horas do Objetivo Realizadas
		 * 3. Nome do Curso
		 * 
		 * */
		Bundle gotBasket = getIntent().getExtras();
        String courseName = (String)gotBasket.getCharSequence("course");
        int[] planned = gotBasket.getIntArray("planned");
        int[] realized = gotBasket.getIntArray("realized");
        String[] goals = (String[])gotBasket.getCharSequenceArray("goals");
        course_id = gotBasket.getInt("course_id");
        
        for (int i = 0; i < planned.length; i++) {
			Log.i("PLANNEDD: ", planned[i]+"");
		}
        
        for (int i = 0; i < realized.length; i++) {
			Log.i("REALIZED: ", realized[i]+"");
		}
        for (int i = 0; i < goals.length; i++) {
        	Log.i("GOALS: ", goals[i]);
		}
        /**
         * t = Lista com a Quantidade de horas planejadas e realizadas ou valor da coluna da coordenada Y ou Tempo de estudo.
         * o = Listagem dos Objetivos com seus 4 primeiros caracteres para a coordenadas X.
         * */
        String t = new String("");
        String o = new String("");
        
        
        for (int i = 0; i < goals.length; i++) {
        	if (i == (goals.length - 1)) {
        		t += planned[i]+"|";
			}else{
				t += planned[i]+",";
			}
        	
		}
        
		for (int i = 0; i < goals.length; i++) {
			if (i == (goals.length - 1)) {
	    		t += realized[i];
			}else{
				t += realized[i]+",";
			}
		}
        
        
		Log.i("T: ", t);
		Log.i("O: ", o);
		
		/**
		 * 
		 * URL de teste: http://chart.apis.google.com/chart?cht=bvs&chxt=x,y&chd=t:20,40,60,80,30,34,54,90&chco=083d23|c60017&chs=600x480&chxl=0:|Tir|Tir|Faz|Faz|Dar|Dar|Est|Est&chxs=0,FFFFFF|1,FFFFFF&chtt=OBJETIVOS|Course%2001&chts=FFFFFF,16,l&chdl=Planejado|Realizado&chdlp=r|0,1&chdls=FFFFFF,12&chf=bg,s,238e23
		 * 
		 * */
		Log.i("CHART", t.toString());
		Log.i("CHART", o);
		String url = "http://chart.apis.google.com/chart?cht=bvs&chxt=x,y&chd=t:"+t.toString()+"&chco=0e2e05,b8ffa5&chs=600x480&chxl=0:"+o+"&chxs=0,FFFFFF|1,FFFFFF&chtt=OBJETIVOS X HORAS|"+courseName+"&chts=FFFFFF,16,l&chdl=Planejado|Realizado&chdlp=r|0,1&chdls=FFFFFF,12&chf=bg,s,238e23";
		Log.i("URLLLL: ", url);
		googleChartView.loadUrl(url);
	}
	
	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu)
	{
		super.onCreateOptionsMenu(menu);
		MenuItem itemNovo = menu.add(0, NOVO, 0, "Novo");
		itemNovo.setIcon(R.drawable.novoitem);
		
		MenuItem itemList = menu.add(0,LISTAGEM, 0, "Listagem");
		itemList.setIcon(R.drawable.listagem);
		/*
		MenuItem itemgraf = menu.add(0, GRAFICO, 0, "Gráficos");
		itemgraf.setIcon(R.drawable.graficos);
		*/
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		switch (item.getItemId()) 
		{
			case NOVO:
				itNewGoal = new Intent("FORM_GOAL");
				Bundle newGoalBasket = new Bundle();
				newGoalBasket.putInt("course_id", course_id);
				itNewGoal.putExtras(newGoalBasket);
				startActivity(itNewGoal);
				return true;
	
			case LISTAGEM:
				itListGoals = new Intent("HOME_GOAL");
				Bundle homeGoalBasket = new Bundle();
				homeGoalBasket.putInt("course_id", course_id);
				itListGoals.putExtras(homeGoalBasket);
				startActivity(itListGoals);
				return true;
				
			/*case GRAFICO:
				ArrayList<Goal> goalList = facadeAmadeusDroid.getAllGoalByCourse(course_id);
				if (!goalList.isEmpty()) {
					chartIntent = new Intent("CHART_GOAL");
					Course c = facadeAmadeusDroid.getCourse(course_id);
					ArrayList<Task> taskList;
					int[] plannedList = new int[goalList.size()];
					int[] realizedList = new int[goalList.size()];
					String[] goals = new String[goalList.size()];
					
					GregorianCalendar beginDate;
					GregorianCalendar endDate;
					for (int i = 0; i < goalList.size(); i++) {
						beginDate = Util.stringDatetoGregorianCalendar(goalList.get(i).getBeginDate());
						endDate = Util.stringDatetoGregorianCalendar(goalList.get(i).getEndDate());
						plannedList[i] = Util.getIntervalHours(beginDate, endDate);
						plannedList[i] = (int)(plannedList[i]/24)*(goalList.get(i).getAvgStudyTime()/60);
						goals[i] = goalList.get(i).getDefinition().substring(0, 4);
						taskList = facadeAmadeusDroid.getAllTasksByGoal(goalList.get(i).getId());
						for (int j = 0; j < taskList.size(); j++) {
							int[] temp = new int[taskList.size()];
							beginDate = Util.stringDatetoGregorianCalendar(taskList.get(j).getBeginDate());
							endDate = Util.stringDatetoGregorianCalendar(taskList.get(j).getEndDate());
							temp[j] = Util.getIntervalHours(beginDate, endDate);
							for (int k = 0; k < temp.length; k++) {
								realizedList[i] += temp[k];
							}
						}
					}
					
					Bundle chartBasket = new Bundle();
					chartBasket.putCharSequence("course", c.getName());
					chartBasket.putIntArray("planned", plannedList);
					chartBasket.putIntArray("realized", realizedList);
					chartBasket.putCharSequenceArray("goals", goals);
					chartIntent.putExtras(chartBasket);
					startActivity(chartIntent);
					return true;
				}else{
					AlertDialog.Builder dialog = new AlertDialog.Builder(this);
					dialog.setMessage("Antes você deve definir seu objetivo e planejar suas tarefas para poder visualiza-las no Gráfico.");
					dialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							try {
								this.finalize();
							} catch (Throwable e) {
								e.printStackTrace();
							}
							
						}
					});
					dialog.show();
				}*/
		}
		
		return false;
	}
}
